#!/usr/bin/env python
# -*- coding=utf-8 -*-
from tornado import web as tor_web
from tornado import ioloop
from tornado import httpserver
from tornado import options

options.define("port", default=8000, type=int,
               help="run server on the given port.")  # 定义服务器监听端口选项
options.define("itcast", default=[], type=str, multiple=True,
               help="itcast subjects.")  # 无意义，演示多值情况


class IndexHandler(tor_web.RequestHandler):
    """
    IndexHandler
    """

    def get(self):
        python_url = self.reverse_url("python_url")
        self.write('<a href="%s">itcast</a>' % python_url)


class ItCatstHandler(tor_web.RequestHandler):
    def initialize(self, subjects):
        self.subjects = subjects

    def get(self):
        self.write(self.subjects)


if __name__ == '__main__':
    options.parse_command_line()  # 解析命令行，
    # 将命令行中的参数传入到全局变量tornado.options.options中对应的变量中

    print(options.options.itcast)
    app = tor_web.Application([
        (r"/", IndexHandler),
        (r"/cpp", ItCatstHandler, {"subjects": "c++"}),
        tor_web.url(r"/language/python", ItCatstHandler,
                    {"subjects": "python"}, name="python_url"),
    ], debug=True)

    http_server = httpserver.HTTPServer(app)
    print(options.options.port)
    http_server.listen(options.options.port)

    ioloop.IOLoop.current().start()
